package cn.yunhe.servlet;

import cn.yunhe.dao.ILiuYanDao;
import cn.yunhe.dao.impl.LiuYanDao;
import cn.yunhe.entity.Admin;
import cn.yunhe.entity.LiuYan;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class LiuYanServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();

        //获取各个值
        String title = request.getParameter("title");
        String context = request.getParameter("context");
        Admin admin = (Admin) session.getAttribute("admin");
        //创建留言对象
        LiuYan liuYan = new LiuYan(title, context, admin);
        //开始添加
        ILiuYanDao liuYanDao = new LiuYanDao();
        if (liuYanDao.addLiuYan(liuYan)) {
            out.print("<script>alert('发表成功!!!');location.href='LiuYanListServlet';</script>");
        } else {
            out.print("<script>alert('发表失败,请联系管理员!!!');location.href='LiuYanListServlet';</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
